package com.gdsz.libbasic.widget

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewConfiguration
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs


class PagerRecyclerView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr) {
    private val mTouchSlop: Int

    init {
        mTouchSlop = ViewConfiguration.get(context).scaledTouchSlop
    }

    private var mLastX = 0f
    private var mLastY = 0f



    override fun dispatchTouchEvent(e: MotionEvent): Boolean  {
        when (e.action) {
            MotionEvent.ACTION_DOWN -> {
                mLastX = e.x
                mLastY = e.y
            }
            MotionEvent.ACTION_MOVE -> {
                val dx = e.x - mLastX
                val dy = e.y - mLastY
                if (abs(dy) > abs(dx)) {
                    // 垂直滑动，请求ViewPager2不拦截事件
                    parent.requestDisallowInterceptTouchEvent(true)
                }
                mLastX = e.x
                mLastY = e.y
            }
            MotionEvent.ACTION_UP -> {
                // ...
            }
        }
        return  super.dispatchTouchEvent(e)
    }


}